//
//  ViewController.m
//  XHImageMostColor
//
//  Created by 曾 宪华 on 14-1-15.
//  Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群（142557668）. All rights reserved.
//

#import "ViewController.h"
#import "UIImage+XHImageMostColor.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 200, 100, 100)];
    [self.view addSubview:self.imageView];
    
    
    
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    btn.frame = CGRectMake(20, 20, 60, 60);
    [btn addTarget:self action:@selector(onBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"1" forState:UIControlStateNormal];
    btn.tag = 1;
    [self.view addSubview:btn];
    
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn addTarget:self action:@selector(onBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(100, 20, 60, 60);
    [btn setTitle:@"2" forState:UIControlStateNormal];
    btn.tag = 2;
    [self.view addSubview:btn];
    
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn addTarget:self action:@selector(onBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(180, 20, 60, 60);
    [btn setTitle:@"3" forState:UIControlStateNormal];
    btn.tag = 3;
    [self.view addSubview:btn];
}

- (void)onBtn:(UIButton*)btn {
    UIImage *image = nil;
    switch (btn.tag) {
        case 1:
            image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", btn.tag]];
            break;
        case 2:
        case 3: {
            image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg", btn.tag]];
            break;
        }
        default:
            break;
    }
    self.imageView.image = image;
    
    UIColor *mostColor = [image mostColor];
    self.view.backgroundColor = mostColor;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
